package org.bdware.bdoa.authentication.serviceImpl;


import org.bdware.bdoa.authentication.JwtConfig;
import org.bdware.bdoa.authentication.exception.JwxException;
import org.bdware.bdoa.authentication.service.JwtService;
import org.bdware.bdoa.authentication.utils.JwxUtils;

//通过对称加密来生成和解析JWT令牌
public class HMACJwtServiceImpl implements JwtService {
    private JwtConfig jwtConfig;

    public HMACJwtServiceImpl(JwtConfig jwtConfig) {
        this.jwtConfig = jwtConfig;
    }

    @Override
    public String genKey() {
        String key = jwtConfig.getHmacKey();
        if (JwxUtils.isEmpty(key)) {
            throw new JwxException(JwxUtils.KEY_GEN_ERROR, new NullPointerException("HMAC need a key"));
        }
        return key;
    }

    @Override
    public String sign(String info) {
        return JwxUtils.signClaimByHMAC(info, genKey(), jwtConfig);
    }

    @Override
    public String verify(String publicKeyStr, String token) {
        return JwxUtils.verifyClaimByHMAC(token, genKey(), jwtConfig);
    }

    @Override
    public String verify(String publicKeyStr, String source, String token) {
        return null;
    }

}
